package com.frank.oj.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.frank.model.entity.msg.AdminSysNotice;
import com.frank.model.entity.msg.UserSysNotice;
import com.frank.oj.service.entity.msg.AdminSysNoticeEntityService;
import com.frank.oj.service.entity.msg.UserSysNoticeEntityService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author frank
 * @Date 2024/3/27
 */
@Component
public class AdminNoticeManager {

    @Resource
    private AdminSysNoticeEntityService adminSysNoticeEntityService;
    @Resource
    private UserSysNoticeEntityService userSysNoticeEntityService;
    /**
     * 对单一用户进行发送
     * @param adminId 管理员
     * @param recipientId 接收方
     * @param title 标题
     * @param content 内容
     * @param type 类型：全部用户All，指定单个用户Single，管理员Admin
     */
    @Transactional(rollbackFor = Exception.class)
    @Async
    public void addSingleNoticeToUSer(String adminId, String recipientId, String title, String content, String type) {
        AdminSysNotice adminSysNotice = new AdminSysNotice();
        adminSysNotice.setAdminId(adminId)
                .setRecipientId(recipientId)
                .setTitle(title)
                .setContent(content)
                .setType(type)
                .setState(true);

        boolean isOk = adminSysNoticeEntityService.save(adminSysNotice);
        if (isOk){
            UserSysNotice userSysNotice = new UserSysNotice();
            userSysNotice.setRecipientId(recipientId)
                    .setSysNoticeId(adminSysNotice.getId())
                    .setType(type);
            userSysNoticeEntityService.save(userSysNotice);
        }
    }

    /**
     * 群发
     * @param adminId 管理员
     * @param recipientIdList 接收方列表
     * @param title 标题
     * @param content 内容
     * @param type 类型
     */
    @Transactional(rollbackFor = Exception.class)
    @Async
    public void addSingleNoticeToBatchUser(String adminId,
                                           List<String> recipientIdList,
                                           String title,
                                           String content,
                                           String type) {
        if (CollectionUtils.isEmpty(recipientIdList)) {
            return;
        }
        AdminSysNotice adminSysNotice = new AdminSysNotice();
        adminSysNotice.setAdminId(adminId)
                .setType("Single")
                .setTitle(title)
                .setContent(content)
                .setState(true);
        boolean isOk = adminSysNoticeEntityService.save(adminSysNotice);
        if (isOk) {
            List<UserSysNotice> userSysNoticeList = new ArrayList<>();
            for (String recipientId : recipientIdList) {
                UserSysNotice userSysNotice = new UserSysNotice();
                userSysNotice.setRecipientId(recipientId)
                        .setSysNoticeId(adminSysNotice.getId())
                        .setType(type);
                userSysNoticeList.add(userSysNotice);
            }
            userSysNoticeEntityService.saveBatch(userSysNoticeList);
        }
    }

    /**
     * 同步通知给新用户
     * @param uidList 新用户列表
     */
    @Async
    public void syncNoticeToNewRegisterBatchUser(List<String> uidList) {
        QueryWrapper<AdminSysNotice> adminSysNoticeQueryWrapper = new QueryWrapper<>();
        adminSysNoticeQueryWrapper
                .eq("type", "All")
                .le("gmt_create", new Date())
                .eq("state", true);
        List<AdminSysNotice> adminSysNotices = adminSysNoticeEntityService.list(adminSysNoticeQueryWrapper);
        if (adminSysNotices.isEmpty()) {
            return;
        }
        List<UserSysNotice> userSysNoticeList = new ArrayList<>();
        for (String uid : uidList) {
            for (AdminSysNotice adminSysNotice : adminSysNotices) {
                UserSysNotice userSysNotice = new UserSysNotice();
                userSysNotice.setType("Sys")
                        .setSysNoticeId(adminSysNotice.getId())
                        .setRecipientId(uid);
                userSysNoticeList.add(userSysNotice);
            }
        }
        userSysNoticeEntityService.saveOrUpdateBatch(userSysNoticeList);
    }
}
